/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rw.ac.kiosc.jdbc.school.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import rw.ac.kiosc.jdbc.school.dao.StudentDAO;
import rw.ac.kiosc.jdbc.school.orm.Department;
import rw.ac.kiosc.jdbc.school.orm.Student;

/**
 *
 * @author antoine
 */
public class StudentHibernateDAOImpl extends DAO implements StudentDAO {

    public static StudentHibernateDAOImpl instance;

    public static StudentHibernateDAOImpl getInstance() {
        if (instance == null) {
            instance = new StudentHibernateDAOImpl();
        }
        return instance;
    }

    private StudentHibernateDAOImpl() {
    }

    public Student saveStudent(Student student) {
        try {
            begin();
            int id = (Integer) getSession().save(student);
            commit();
            Student std = getStudentById(id);
            return std;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public Student updateStudent(Student student) {
        try {
            begin();
            getSession().update(student);
            commit();
            return student;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public Student saveOrUpdateStudent(Student student) {
        try {
            begin();
            getSession().saveOrUpdate(student);
            commit();
            return student;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public boolean deleteStudent(Student student) {
        try {
            begin();
            getSession().delete(student);
            commit();
            return true;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return false;
        }
    }

    public Student getStudentById(int studentId) {
        try {
            begin();
            Query q = getSession().createQuery("from Student where studentId = :ref");
            q.setInteger("ref", studentId);
            Student std = (Student) q.uniqueResult();
            commit();
            return std;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public List<Student> getAllStudents() {
        try {
            begin();
            List<Student> students = (List<Student>) getSession().createQuery("from Student").list();
            commit();
            return students;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return new ArrayList<Student>();
        }
    }

    public Student getStudentByRegistrationNumber(String registrationNumber) {
        try {
            begin();
            Query q = getSession().createQuery("from Student where registrationNumber = :ref");
            q.setString("ref", registrationNumber);
            Student std = (Student) q.uniqueResult();
            commit();
            return std;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return null;
        }
    }

    public List<Student> getStudentsByName(String name) {
        try {
            begin();
            Query q = getSession().createQuery("from Student where name like :ref");
            q.setString("ref", name);
            List<Student> students = (List<Student>) q.list();
            commit();
            return students;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return new ArrayList<Student>();
        }
    }

    public List<Student> getStudentsByDepartment(Department department) {
        try {
            begin();
            Query q = getSession().createQuery("from Student where department = :ref");
            q.setSerializable("ref", department);
            List<Student> students = (List<Student>) q.list();
            commit();
            return students;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return new ArrayList<Student>();
        }
    }

    public List<Student> getStudentsByYearOfStudy(int yos) {
        try {
            begin();
            Query q = getSession().createQuery("from Student where yearOfStudy = :ref");
            q.setInteger("ref", yos);
            List<Student> students = (List<Student>) q.list();
            commit();
            return students;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return new ArrayList<Student>();
        }
    }

    public List<Student> getStudentsByYearOfStudyAndDepartment(int yos, Department department) {
        try {
            begin();
            Query q = getSession().createQuery("from Student where yearOfStudy = :ref and department = :refone");
            q.setInteger("ref", yos);
            q.setSerializable("refone", department);
            List<Student> students = (List<Student>) q.list();
            commit();
            return students;
        } catch (Exception ex) {
            System.out.println(ex.toString());
            ex.printStackTrace();
            rollback();
            return new ArrayList<Student>();
        }
    }
}
